var teacherQueryUrl = basePath + "/organize/workgroup/manage/query";
var teacherUpdateUrl = basePath + "/organize/workgroup/manage/update";

var dataGrid;
$(function() {
	loadData();
});

function loadData() {
	dataGrid = $('#teacherManageDg').datagrid({
		url : teacherQueryUrl,
		toolbar: '#tb',
		fit : true,
		fitColumns : true,
		border : true,
		pagination : true,
		idField : 'id',
		pageSize : config.pageSize,
		autoRowHeight : 140,
		pageList : config.pageList,
		nowrap : true,
		striped : true,
		rownumbers : true,
		singleSelect: true,
		columns : [ [  {
			field : 'id',
			width : 20,
			align : "center",
			checkbox : true
		}, {
			field : 'ab',
			title : '模块id',
			resizable : true,
			align : "center",
			width : 20,
			formatter : function(value,row,index){
				return row.id;
			}
		}, {
			field : 'groupName',
			title : '组名',
			resizable : true,
			align : "center",
			width : 20,
			editor:{type:'validatebox',options:{required:true}}
		}, {
			field : 'parentId',
			title : '父级id',
			width : 10,
			align : "center",
			editor:{type:'numberbox',options:{required:true}}
		}, {
			field : 'groupCode',
			title : '组code',
			width : 20,
			align : "center",
			editor:{type:'validatebox'}
		}, {
			field : 'groupType',
			title : '组类型',
			width : 20,
			align : "center",
			formatter: function(row,value,index) {
				if(value == 'sys') return "系统组";
				return "自定义组";
			},
			editor:{
				type:'combobox',
				options: {  
                    data: [{"value": "sys", "text": "系统组"},{"value": "custom", "text": "自定义组"}],  
                    editable: false,  
                    panelHeight:70,  
                    required: true  
                }  
			}
		}, {
			field : 'ids',
			title : '身份',
			resizable : true,
			align : "center",
			width : 50,
			formatter : function(value,row,index){
				return row.positionText;
			},
			editor:{
				type:'combobox',
				options:{
					valueField:'id',
					textField:'position',
					groupField:'depart',
					onBeforeLoad:function(param){
			            var r = $('#teacherManageDg').datagrid('getSelected');
			            param.groupCode = r.groupCode;
			        },
					method:'get',
					url: basePath + "/organizeGroup/comboBox",
					multiple:true,
					editable:false,
					required:true
				}
			}
		}, {
			field : 'weight',
			title : '顺序',
			width : 10,
			align : "center",
			editor:{type:'numberbox',options:{required:true}}
		}] ],
		onAfterEdit : function(rowIndex, rowData, changes){
			if(rowData.id){
				changes.id = rowData.id;
				addmodify(changes)
			}else {
				addmodify(rowData);
			}
		}
	});
}

function reload() {
	dataGrid.datagrid("clearSelections");
	dataGrid.datagrid('reload');
}

function searchForm(formId) {
	dataGrid.datagrid("clearSelections");
	dataGrid.datagrid('load', $.serializeObject($('#' + formId)));
}
function resetForm(formId) {
	$("#" + formId)[0].reset();
}

function save(id) {
	if(dgEditIndex[id] != null){
		$('#' + id).datagrid('endEdit', dgEditIndex[id]);
		dgEdit[id] = null;
	}
}

function addmodify(data) {
	$.ajax({
	   type: "POST",
	   url: teacherUpdateUrl,
	   data: data,
	   success: function(res){
		   if (res.code == '000'){
			   reload();
			   $.messager.alert('提示', '变更成功', 'info');
		   }else{
			   $.messager.alert('提示', '变更失败', 'error');
			   errorEdit("teacherManageDg");
		   }
		}
	});
}

